package robosym.visual.emulator;

import java.util.ArrayList;
import java.util.Random;
import robosym.utils.Geo;
import robosym.utils.Geo;
import robosym.visual.Barrier;

/**
 *
 * @author TR
 */
public class Map {
    
    public static float MINX = 0;
    public static float MINY = 0;       
    public static float MAXX = 800;
    public static float MAXY = 800;
    
    public static float MINDIAMETER = 10;
    public static float MAXDIAMETER = 30;
    
    public static Map map = null;

    private ArrayList<Barrier> barriers;
    
    public static Map getInstance(){
        if(map == null){
            Map m = new Map();
            m.initRandom(50);
            map = m;
        }
        return map;
    }
    
    protected void initRandom(int barriersCount){
        Random random = new Random();
        this.barriers = new ArrayList<Barrier>();
        Barrier barrier;
        for(int i = 0; i < barriersCount; i++){
            barrier = new Barrier(
                    MINX + ( (MAXX-MINX) * random.nextFloat()), 
                    MINY + ( (MAXY-MINY) * random.nextFloat()), 
                    MINDIAMETER + ( (MAXDIAMETER-MINDIAMETER) * random.nextFloat())
                    );
            this.getBarriers().add(barrier);
        }
    }

    public void addBarriers(ArrayList<Barrier> barriers) {
        this.getBarriers().addAll(barriers);
    }
    
    public void addBarrier(Barrier barrier){
        this.getBarriers().add(barrier);
    }
    
    /**
     * returns object located in sector with
     * top in (x,y)
     * and start-end angles
     * and located near than custom distance
     * 
     * @param x top of sector
     * @param y top of sector
     * @param startAngleRad
     * @param endAngleRad
     * @param distance
     * @return
     */
    public Barrier[] getBarriersByFilter(float x, float y, float startAngleRad,
            float endAngleRad, float distance){
        float dx,dy;
        
        ArrayList<Barrier> result = new ArrayList<Barrier>();
        
        for(Barrier b : getBarriers()){
            dx = b.getX() - x;
            dy = b.getY() - y;
            
            float angle = Geo.angleOfVectorRad(dx, dy);
            
            if(Geo.distance(dx, dy) < distance && //distance check
               startAngleRad < angle && //angle check
                    angle < endAngleRad
                    ){
                result.add(b);
            }
        }
        
        return result.toArray(new Barrier[result.size()]);
    }

    /**
     * @return the barriers
     */
    public ArrayList<Barrier> getBarriers() {
        return barriers;
    }
    
}
